package com.woniuxy.qiantai.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.dal.entity.History;
import com.woniuxy.dal.entity.Ranges;
import com.woniuxy.servicelayer.HistoryService;
import com.woniuxy.servicelayer.RangeService;
import com.woniuxy.servicelayer.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 历史记录表(History)表控制层
 *
 * @author makejava
 * @since 2023-03-27 22:21:38
 */
@RestController
@RequestMapping("history")
public class HistoryController {
    /**
     * 服务对象
     */
    @Resource
    private HistoryService historyService;

    @Autowired
    private RangeService rangeService;

    @RequestMapping("selectHistoryByUserId")
    public Page<History> selectHistoryByUserId(@RequestBody Map<String, Object> param) {
        Object token = param.get("token");
        Integer userId = (Integer) JwtUtil.parseValue((String) token, "userId");
        Integer currentPage = (Integer) param.get("currentPage");
        Integer pageSize = (Integer) param.get("pageSize");
        Integer status = (Integer) param.get("status");
        return historyService.selectHistoryByUserId(userId, status, currentPage, pageSize);
    }

//    @RequestMapping("updateHistory")
//    public Integer updateHistory(@RequestBody Map<String, Object> param) {
//        Object token = param.get("token");
//        Integer userId = (Integer) JwtUtil.parseValue((String) token, "userId");
//        Integer rangeId = (Integer) param.get("rangeId");
//        Integer status = (Integer) param.get("status");
//        Integer result = historyService.updateHistory(rangeId, userId, status);
//        if (result == 1){
//            return 200;
//        }else {
//            return null;
//        }
//    }

    @RequestMapping("addHistory")
    public Integer addHistory(@RequestBody Map<String, Object> param) {
        Object token = param.get("token");
        Integer userId = (Integer) JwtUtil.parseValue((String) token, "userId");
        Integer rangeId = (Integer) param.get("rangeId");
        Integer status = (Integer) param.get("status");
        Integer result = historyService.addHistory(rangeId, userId, status);
        if (result == 1){
            return 200;
        }else {
            return null;
        }
    }

    /**
     * 获取靶场详细信息
     *
     * @param params
     * @return
     */
    @PostMapping("getExperimentDetail")
    public Ranges getExperimentDetail(@RequestBody Map<String, Object> params) {
        Object id = params.get("id");
        return rangeService.getRange(id);
    }
}